global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap log using ${log_dir}/Table_A29_shocklevel_regression.log, replace name(tabl)

capture noi { 


    * BHJ Equivalent shock level IV reg

    * parameters
    local build_data = 1 // set to 1 if to residualize the dep and indep vars


    
    * --------------------------------------
    * a) Build country level eq data
    * -------------------------------------
    local restrictions "baseline"

    foreach restr of local restrictions { 
        use ${final_dir}/regression_dataset_from1970_tfacit1_avglog.dta, clear

        * Define variables
        local ss_vars LSW HSW GDPGAP VAEMP GDPPC
        local stocks stockown stockownzero stockother stockotherzero
        local spillovers spilloversown_og spilloversother_og spilloversownzero_og spilloversotherzero_og
        local F_IY_CY lse_id yearindustry yearctry
        local depvars arcsinh_auto95_bia 

        * Define control and FE specifications to residualize
        local ct1 
        local fe1 

        local ct2 
        local fe2 `F_IY_CY'

        local ct3 `spillovers' `stocks'
        local fe3 `F_IY_CY'

        * Residualize and take s_n-weighted country-year mean of firm-level data
        ssaggregate `depvars' `ss_vars', /// 
            n(country) t(year) s(s_nt) sfilename(${dataset_dir}/bhj/lseid_year_ctryweights_long.dta) l(lse_id) ///
            controls("`ct1'" "`ct2'" "`ct3'") ///
            absorb("`fe1'" "`fe2'" "`fe3'")
            
        foreach v of local ss_vars {
            _strip_labels `v'*
            ren `v'* z`v'*
        }

        * Add country-year level information, build skill premium, save
        mmerge country year using  ${dataset_dir}/bhj/country_year_macrovariables.dta, unmatched(master)
        drop _m
        forvalues r = 1/3 {  
            gen zISP`r' = zLSW`r' - zHSW`r'
        }
        * gen gISP = gLSW - gHSW 
        egen country_id = group(country)
        tsset country_id year
        tempfile shocklevel_resid_`restr'
        save `shocklevel_resid_`restr''
    }



    * --------------------------------------
    * b) Run Regressions 
    * -------------------------------------
    qui do ${code_dir}/config/tabletools.do


    local stocks stockown stockownzero stockother stockotherzero
    local spillovers spilloversown_og spilloversother_og spilloversownzero_og spilloversotherzero_og
    local F_IY_CY lse_id yearindustry yearctry
    local depvars arcsinh_auto95_bia 
    local serrors cclustered 
    local lhs_types "skillprem"
    local restrictions "baseline"


    foreach restr of local restrictions { 
        foreach depvar of local depvars { 
            foreach lt of local lhs_types { 
                if ("`lt'" == "skillprem") local indep "ISP"
                if ("`lt'" == "separate") local indep "LSW HSW"
                foreach setype of local serrors { 
                    if ("`setype'" == "nwl1") local serror "bw(2)"
                    if ("`setype'" == "nwl2") local serror "bw(3)"
                    if ("`setype'" == "robust") local serror "robust"
                    if ("`setype'" == "cclustered") local serror "cluster(country_id)"

                    * A: FIRM LEVEL
                    estimates clear
                    use ${final_dir}/regression_dataset_from1970_tfacit1_avglog.dta, clear
                    if "`restr'" == "atleasttwoauto" { 
                        bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
                        bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
                        drop _total_${depvar}_${ttt}_1995
                        keep if total_${depvar}_${ttt}_1995 >= 2
                    }
                    gen ln05p_auto95_bia = log(0.5 + auto95_bia)
                    gen ln01p_auto95_bia = log(0.1 + auto95_bia)

                    * Col 1: Firm-level: Poisson auto95 = ISP + stocks + spills | F_IY_CY
                    ppmlhdfe auto95_bia `indep' `stocks' `spillovers', absorb(`F_IY_CY') vce(cluster lse_id)
                    estadd local has_stockspill "\yes"
                    estadd local f "\yes"
                    estadd local iy "\yes"
                    estadd local cy "\yes"
                    estadd local se "Firm-clustered"
                    estadd local est "{Poisson}"
                    estadd local obs "{\num{`e(N)'}}"
                    estadd local firms "{\num{`e(N_clust)'}}"
                    estimates store col_1

                    * Col 2: Firm-level: Arcsinh auto95 = ISP + stocks + spills | F_IY_CY
                    reghdfe `depvar' `indep' `stocks' `spillovers', absorb(`F_IY_CY') vce(cluster lse_id)
                    estadd local has_stockspill "\yes"
                    estadd local f "\yes"
                    estadd local iy "\yes"
                    estadd local cy "\yes"
                    estadd local se "Firm-clustered"
                    estadd local est "{Linear (arcsinh)}"
                    estadd local obs "{\num{`e(N)'}}"
                    estadd local firms "{\num{`e(N_clust)'}}"
                    estimates store col_2

                    * A: COUNTRY LEVEL

                    * Col 3: Country-level: Arcsinh auto95 = ISP + stocks + spills | F_IY_CY
                    use `shocklevel_resid_`restr'', clear
                    if ("`lt'" == "skillprem") ivreg2 `depvar'3 (zISP3 = gISP) [aw=s_n], `serror' 
                    if ("`lt'" == "separate") ivreg2 `depvar'3 (zLSW3 zHSW3 = gLSW gHSW) [aw=s_n], `serror' 
                    estadd local has_stockspill "\yes"
                    estadd local f "\yes"
                    estadd local iy "\yes"
                    estadd local cy "\yes"
                    estadd local se "Country-clustered"
                    estadd local est "{Linear (arcsinh)}"
                    estadd local obs "{\num{`e(N)'}}"
                    estadd local firms "{\num{`e(N_clust)'}}"
                    estimates store col_3

                    * Col 4: Country-level: Arcsinh auto95 = ISP + stocks + spills + labor productivity + gdpgap | F_IY_CY
                    if ("`lt'" == "skillprem") ivreg2 `depvar'3 (zISP3 zVAEMP3 zGDPGAP3 = gISP gVAEMP gGDPGAP) [aw=s_n], `serror'  
                    if ("`lt'" == "separate") ivreg2 `depvar'3 (zLSW3 zHSW3 zVAEMP3 zGDPGAP3 = gLSW gHSW gVAEMP gGDPGAP) [aw=s_n], `serror' 
                    estadd local has_stockspill "\yes"
                    estadd local f "\yes"
                    estadd local iy "\yes"
                    estadd local cy "\yes"
                    estadd local se "Country-clustered"
                    estadd local est "{Linear (arcsinh)}"
                    estadd local obs "{\num{`e(N)'}}"
                    estadd local firms "{\num{`e(N_clust)'}}"
                    estimates store col_4

                    * Col 5: Country-level: Arcsinh auto95 = ISP | F_IY_CY
                    if ("`lt'" == "skillprem") ivreg2 `depvar'2 (zISP2 = gISP) [aw=s_n], `serror' 
                    if ("`lt'" == "separate") ivreg2 `depvar'2 (zLSW2 zHSW2 = gLSW gHSW) [aw=s_n], `serror' 
                    estadd local has_stockspill "\no"
                    estadd local f "\yes"
                    estadd local iy "\yes"
                    estadd local cy "\yes"
                    estadd local se "Country-clustered"
                    estadd local est "{Linear (arcsinh)}"
                    estadd local obs "{\num{`e(N)'}}"
                    estadd local firms "{\num{`e(N_clust)'}}"
                    estimates store col_5

                    setlabels

                    dhoztab * using ${tab_dir}/appendix/Table_A29_shocklevel_regression.tex, b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines showdepvarinfo nomtitles sfmt(a1) scalars("est \noalign{\medskip} Estimator" "has_stockspill Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Firms / Countries") label mgroups("Firm-level" "Country-level", pattern(1 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(_cons *stock* *spill*) noobs rename(zISP2 ISP zISP3 ISP zGDPGAP3 GDPGAP zVAEMP3 VAEMP zGDPPC3 GDPPC zLSW2 LSW zHSW2 HSW zLSW3 LSW zHSW3 HSW) ///
                        depvar("Auto95") ///
                        notes("This table reports shock-level equivalent regressions. The coefficients are estimated with conditional Poisson fixed effect regressions (HHG) in column 1 and OLS in columns 2--5. The dependent variable in columns 2--5 is the arcsinh transformation of auto95 innovations. Standard errors are reported in parentheses. Standard errors are clustered at the firm-level in columns 1 and 2 and country-level clustered in columns 3--5. Columns 3--5 run equivalent shock-level regressions following Borusyak, Hull and Jaravel (2022, BHJ) (see text for details).  All regresions include firm fixed effects, industry-year fixed effects and country-year fixed effects.")
                }   
            }
        }
    }
    }
    *again, brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}
cap log close tabl